﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using NHunspell;

namespace Modulo3
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Count() != 4)
            {
                Console.WriteLine("Args error!");
                return;
            }
            var dataEntreFileName = args[0];
            var dataOutputFileName = args[1];
            var logFileName = args[2];
            var errFileName = args[3];

            int entryRegisterCout = 0;
            int procesedResgisterCount = 0;
            int outputRegisterCount = 0;

            var outputFile = new StreamWriter(dataOutputFileName);
            var logFile = new StreamWriter(logFileName);
            var errFile = new StreamWriter(errFileName);

            logFile.WriteLine("==== Module #3====");
            logFile.WriteLine(DateTime.Now.ToString());
            errFile.WriteLine("==== Module #3====");
            errFile.WriteLine(DateTime.Now.ToString());

            // poner en Repo Fallas I\TP 2\Proyecto\TP2\TP2\bin\Debug los .dll de HNunspell
            // y agregar la referencia del NHunspell.dll al proyecto
            var hunspell = new Hunspell("es_ES.aff", "es_ES.dic");

            const char Separator = '\n';
            var words = File.ReadAllText(dataEntreFileName, Encoding.UTF8).Split(new[] { Separator });
            foreach (var word in words)
            {
                var stringWord = new String(word.Where(Char.IsLetter).ToArray());
                if (word.Length == 0)
                {
                    continue;
                } 

                entryRegisterCout++;
                List<string> stems = hunspell.Stem(stringWord);
                // TODO : make something better to select stem
                if (stems.Count != 0)
                {
                    var stem = stems.Last();
                    outputFile.WriteLine(stem);
                    outputRegisterCount++;
                    procesedResgisterCount++;
                }else
                {
                    errFile.WriteLine("No stem for word : " + stringWord);
                }
            }

            logFile.WriteLine("Input Register Count : " + entryRegisterCout);
            logFile.WriteLine("Procesed Register Count : " + procesedResgisterCount);
            logFile.WriteLine("Output Register Count : " + outputRegisterCount);
            logFile.WriteLine(DateTime.Now.ToString());
            errFile.WriteLine(DateTime.Now.ToString());

            outputFile.Flush();
            logFile.Flush();
            errFile.Flush();

            outputFile.Close();
            logFile.Close();
            errFile.Close();
        }
    }
}
